package cn.actoncode.boot.module.ai.controller.app.auth;

import cn.actoncode.boot.framework.common.pojo.CommonResult;
import cn.actoncode.boot.framework.security.core.annotations.PreAuthenticated;
import cn.actoncode.boot.framework.web.core.util.WebFrameworkUtils;
import cn.actoncode.boot.module.ai.controller.app.auth.vo.login.RegisterReqVO;
import cn.actoncode.boot.module.ai.controller.app.auth.vo.login.RegisterRespVO;
import cn.actoncode.boot.module.ai.controller.app.auth.vo.login.WebLoginReqVO;
import cn.actoncode.boot.module.ai.controller.app.auth.vo.login.WebLoginRespVO;
import cn.actoncode.boot.module.ai.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.actoncode.boot.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;
import static cn.actoncode.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.actoncode.boot.framework.common.pojo.CommonResult.success;

@Tag(name = "用户认证 API")
@RestController
@RequestMapping("/auth")
@Validated
@Slf4j
public class AppAuthController {

    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/register")
    @Operation(summary = "注册")
    @PreAuthenticated
    public CommonResult<RegisterRespVO> register(@Valid @RequestBody RegisterReqVO reqVO) {
        // 暂时不开放注册
        Long userId = WebFrameworkUtils.getLoginUserId();
        if (userId != 1) {
            throw exception(FORBIDDEN);
        }
        return success(memberUserService.register(reqVO));
    }

    @PostMapping("/login")
    @Operation(summary = "web登录")
    public CommonResult<WebLoginRespVO> login(@Valid @RequestBody WebLoginReqVO reqVO) {
        return success(memberUserService.login(reqVO));
    }

}
